<?php
// +----------------------------------------------------------------------
// | @filename BaseRepository.php
// +----------------------------------------------------------------------
// | @author 落花轻舞问君愁 <mwcx1990@qq.com>
// +----------------------------------------------------------------------
// | @datetime 2021-10-26 16:13:59
// +----------------------------------------------------------------------
// | @Description
// +----------------------------------------------------------------------
namespace Addons\Backend\Repositories;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class BaseRepository
{
    protected $model;

    /**
     * 通过ID查询数据
     * @param $id
     * @return mixed
     */
    public function findById($id)
    {
        return $this->model->find($id);
    }

    /**
     * 创建
     * @param $data
     * @return Builder|Model
     */
    public function create($data)
    {
        return $this->model::create($data);
    }

    /**
     * 更新
     * @param $data
     * @return Builder|Model
     */
    public function update($data)
    {
        $role = $this->model::query()->findOrFail($data['id']);
        $role->fill($data);
        $role->save();
        return $role;
    }

    /**
     * 删除
     * @param $id
     * @return mixed
     */
    public function delete($id)
    {
        return $this->model::destroy($id);
    }


    /**
     * 所有
     * @return Builder[]|Collection
     */
    public function all(){
        return $this->model::query()->get();
    }
}
